<template>
  <div>
    <a-input-search
      v-model="names"
      :placeholder="placeholder"
      readOnly
      unselectable="on"
      @search="onSearchDepUser">
      <a-button slot="enterButton" :disabled="disabled">选择敏感点</a-button>
    </a-input-search>
    <j-select-user-by-dep-modal :sign="sign" ref="selectModal" :modal-width="modalWidth" :multi="false" @ok="selectOK" :user-ids="value" @initComp="initComp"/>
  </div>
</template>

<script>
  import JSelectUserByDepModal from './modal/SelectSensitiveModal'

  export default {
    name: 'SelectSensitive',
    components: {JSelectUserByDepModal},
    props: {
      placeholder: {
        type: String,
        default: '请选择',
        required: false
      },
      modalWidth: {
        type: Number,
        default: 1250,
        required: false
      },
      value: {
        type: String,
        required: false
      },
      disabled: {
        type: Boolean,
        required: false,
        default: false
      },
      multi: {
        type: Boolean,
        default: true,
        required: false
      },
      backUser: {
        type: Boolean,
        default: false,
        required: false
      },
      //1默认查询 2查询本项目下的用户
      sign: {
        type: String,
        default: '1',
        required: false
      }
    },
    data() {
      return {
        ids: "",
        names: ""
      }
    },
    mounted() {
      this.ids = this.value
    },
    watch: {
      value(val) {
        this.ids = val
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    methods: {
      initComp(userNames) {
        this.names = userNames
      },
      //返回选中的用户信息
      backDeparInfo(rows){
        if(this.backUser===true){
          if(rows){
            this.$emit('back', rows);
          }else if(this.userIds && this.userIds.length>0){
            let arr1 = this.ids.split(',')
            let arr2 = this.names.split(',')
            let info = []
            for(let i=0;i<arr1.length;i++){
              info.push({
                value: arr1[i],
                text: arr2[i]
              })
            }
            this.$emit('back', info)
          }
        }
      },
      onSearchDepUser() {
        this.$refs.selectModal.showModal()
      },
      selectOK(rows, idstr) {
        console.log("当前选中用户", rows)
        console.log("当前选中用户ID", idstr)
        if (!rows) {
          this.names = ''
          this.ids = ''
        } else {
          let temp = ''
          for (let item of rows) {
            temp += ',' + item.name
          }
          this.names = temp.substring(1)
          this.ids = idstr
        }
        this.$emit("change", this.ids)
        this.backDeparInfo(rows);
      }
    }
  }
</script>

<style scoped>

</style>